package com.news.push;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.news.ShareUtils;
import com.news.alipay.AlipayAPI;
import com.news.alipay.PayResult;

import java.util.Map;

/**
 * Created by Administrator on 2017/7/6.
 */
public class PayModule extends ReactContextBaseJavaModule {

    private Activity mContext;
    private static final int SDK_PAY_FLAG = 1;
    private Handler mHandle;

    public PayModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = getCurrentActivity();

        Looper.prepare();
        mHandle=new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case SDK_PAY_FLAG: {
                        PayResult payResult = new PayResult((String) msg.obj);
                        /**
                         * 同步返回的结果必须放置到服务端进行验证（验证的规则请看https://doc.open.alipay.com/doc2/
                         * detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=103665&
                         * docType=1) 建议商户依赖异步通知
                         */
                        String resultInfo = payResult.getResult();// 同步返回需要验证的信息

                        String resultStatus = payResult.getResultStatus();
                        // 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
                        if (TextUtils.equals(resultStatus, "9000")) {
                            Toast.makeText(mContext, "支付成功",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            // 判断resultStatus 为非"9000"则代表可能支付失败
                            // "8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
                            if (TextUtils.equals(resultStatus, "8000")) {
                                Toast.makeText(mContext, "支付结果确认中",
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                // 其他值就可以判断为支付失败，包括用户主动取消支付，或者系统返回的错误
                                Toast.makeText(mContext,
                                        "支付失败" + resultStatus, Toast.LENGTH_SHORT)
                                        .show();
                            }
                        }
                        break;
                    }
                }
            };
        };
    }

    @Override
    public String getName() {
        return "payModule";
    }


    @ReactMethod
    public void aLiPay(){
        sendAlipay("9999999999");
    }

    private void sendAlipay(final String orderInfo) {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {

                PayTask task = new PayTask(getCurrentActivity());
                String result = task.pay(orderInfo, true);
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
    }

    /**
     * 支付宝支付异步任务
     *
     * @author Simon
     */
    private class AliPayThread extends Thread {
        @Override
        public void run() {
            String result = AlipayAPI.pay( mContext, "测试的商品",
                    "测试商品的详细描述", "0.01");
            Message msg = new Message();
            msg.what = SDK_PAY_FLAG;
            msg.obj = result;
            mHandle.sendMessage(msg);
        }
    }


}
